package com.helei.consumerListener;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 *
 * @date 2024/10/2 16:55
 */
//同步发送消息注解
@Component


//重试机制
@RocketMQMessageListener(topic ="retry-topic-boot", consumerGroup = "group-retry",
        consumeMode = ConsumeMode.CONCURRENTLY,
        maxReconsumeTimes = 3)
public class ConsumerRetryListenerRocketMq implements RocketMQListener<MessageExt> {

    //MessageExt类型---是消息的所有内容，没有报错就签收，如果报错就拒收。
    @Override
    public void onMessage(MessageExt messageExt) {
       try {
           //处理数据
           handleDb();
       }catch (Exception e){
          // int times = messageExt.getReconsumeTimes();
          // System.out.println("重试次数："+times);
           /*if (times >= 3){
               //将消息记录到特别位置中，比如mysql，文件，人工进行处理
               System.out.println("处理数据失败，需要人工干预处理");
           }else {
               throw new RuntimeException("处理数据失败");
           }*/
           throw new RuntimeException("处理数据失败");
       }
    }

    private void handleDb() {
        int a  = 1/0;
    }

}

